<template>
  <view class="page">
    <Main ref="mainRef" v-show="active == 'main'"></Main>
    <Activity ref="activityRef" v-show="active == 'activity'"></Activity>
    <TreeHole
      v-show="active == 'treeHole'"
      :switchTabbar="switchTabbar"
    ></TreeHole>
    <Message v-show="active == 'message'"></Message>
    <Home v-show="active == 'home'"></Home>
  </view>
  <tabbar :active="active" @switch-tabbar="switchTabbar" />
</template>

<script setup>
import Main from "../../components/tabbar/Main.vue";
import Activity from "../../components/tabbar/Activity.vue";
import TreeHole from "../../components/tabbar/TreeHole.vue";
import Message from "../../components/tabbar/Message.vue";
import Home from "../../components/tabbar/Home.vue";
import Tabbar from "../../components/tabbar/Tabbar.vue";

import { onMounted, ref } from "vue";
import { onReachBottom, onPullDownRefresh } from "@dcloudio/uni-app";

let active = ref("main");

// 定义处理子组件事件的方法
const switchTabbar = (data) => {
  console.log("接收到子组件传递的数据：", data);
  active.value = data.active;
};

onMounted(async () => {
  // console.log(435435435);
});

const mainRef = ref(null);
const activityRef = ref(null);

onReachBottom(() => {
  switch (active.value) {
    case "main": // 处理 main 页面的触底事件
      if (mainRef.value && mainRef.value.onReachBottom) {
        mainRef.value.onReachBottom();
      }
      break;
    case "activity": // 处理 activity 页面的触底事件
      if (activityRef.value && activityRef.value.onReachBottom) {
        activityRef.value.onReachBottom();
      }
      break;
    case "treeHole":
      // 处理 treeHole 页面的触底事件
      break;
    case "message":
      // 处理 message 页面的触底事件
      break;
    case "home":
      // 处理 home 页面的触底事件
      break;
  }
});

onPullDownRefresh(() => {
  switch (active.value) {
    case "main": // 处理 main 页面的触底事件
      if (mainRef.value && mainRef.value.onRefresh) {
        mainRef.value.onRefresh();
      }
      break;
    case "activity": // 处理 activity 页面的触底事件
      if (activityRef.value && activityRef.value.onRefresh) {
        activityRef.value.onRefresh();
      }
      break;
    case "treeHole":
      // 处理 treeHole 页面的触底事件
      uni.stopPullDownRefresh();
      break;
    case "message":
      // 处理 message 页面的触底事件
      uni.stopPullDownRefresh();
      break;
    case "home":
      // 处理 home 页面的触底事件
      uni.stopPullDownRefresh();
      break;
  }
});
</script>

<style scoped lang="less"></style>
